package apitalkroommine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Client extends JFrame {
    private Socket socket;
    private JTextArea chatTextArea;
    private JTextField inputField;
    private PrintWriter toServer;

    public Client() {
        super("Chat Client");
        initializeGUI();
        connectToServer();
    }

    private void initializeGUI() {
        // 设置窗口大小
        setSize(400, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示

        // 创建文本区域显示聊天记录
        chatTextArea = new JTextArea();
        chatTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(chatTextArea);
        add(scrollPane, BorderLayout.CENTER);

        // 创建文本框供用户输入
        inputField = new JTextField();
        inputField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String message = inputField.getText();
                if (!message.trim().isEmpty()) {
                    toServer.println(message);
                    inputField.setText(""); // 清空输入框
                }
            }
        });
        add(inputField, BorderLayout.SOUTH);

        setVisible(true);
    }

    private void connectToServer() {
        try {
            System.out.println("客户端启动");
            socket = new Socket("localhost", 8088);
            System.out.println("客户端连接成功");

            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            toServer = new PrintWriter(bw, true);

            // 发送昵称
            toServer.println("User");

            ServerHandler serverHandler = new ServerHandler();
            Thread t = new Thread(serverHandler);
            t.setDaemon(true);
            t.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class ServerHandler implements Runnable {
        @Override
        public void run() {
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader fromServer = new BufferedReader(isr);
                String message;
                while ((message = fromServer.readLine()) != null) {
                    String finalMessage = message;
                    SwingUtilities.invokeLater(() -> {
                        chatTextArea.append(finalMessage + "\n");
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            Client client = new Client();
            client.setVisible(true);
        });
    }
}
